<?php
/**
 * Created by PhpStorm.
 * User: zouhua
 * Date: 2017/5/5
 * Time: 16:57
 */

namespace app\actions;


use app\models\Activity;
use app\models\Favorite;

class UserFavorite extends UserList {

    public function beforeRun() {
        if (!parent::beforeRun()) {
            return FALSE;
        }

        $uid = $this->user ? $this->user->id : NULL;
        $aidList = 0;
        if ($uid) {
            $favorites = Favorite::find()->where(['uid' => $uid])->asArray()->indexBy('aid')->all();
            $aidList = array_keys($favorites);
        }
        $this->where = ['id' => $aidList];
        $this->modelClass = Activity::className();
        $this->title = '我的收藏';
        $this->layout = 'user';
        $this->_view = 'activity';

        return TRUE;
    }

    public function render($params = [], $view = NULL) {
        $this->controller->view->title = $this->title;
        if (!isset($params['user'])) {
            $params['user'] = $this->user;
        }
        $view = str_replace('_ajax', '', $view);
        return $this->controller->render($params, $view);
    }
}